import { getDocument,GlobalWorkerOptions } from 'pdfjs-dist'
GlobalWorkerOptions.workerSrc=require("pdfjs-dist/build/pdf.worker.entry")

export async function covertPdfToimage(url){
    const renderImages=[]
    const loadingTask=getDocument(url)
    // console.log(loadingTask,"loadingTask")
    const pdf=await loadingTask.promise
    for(let pageNumber=1;pageNumber<=pdf.numPages;pageNumber++){
        const page=await pdf.getPage(pageNumber)
        const viewport=page.getViewport({scale:1})
        // 创建一个canvas元素来渲染pdf页
        const canvas=document.createElement("canvas")
        const context=canvas.getContext("2d")
        canvas.height=viewport.height
        canvas.width=viewport.width

        // 渲染pdf到canvas
        await page.render({canvasContext:context,viewport:viewport}).promise

        // 将canvas转化为图片
        const imgData=canvas.toDataURL()
        // console.log(imgData,"imgData")
        renderImages.push(imgData)
    }
    return renderImages
}